package com.xuecheng.content.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuecheng.commons.constants.CourseConstants;
import com.xuecheng.commons.enums.ErrorCode;
import com.xuecheng.commons.model.dto.BindTeachplanWorkDto;
import com.xuecheng.commons.model.vo.ResponseResult;
import com.xuecheng.commons.model.vo.WorkVo;
import com.xuecheng.content.domain.CourseBase;
import com.xuecheng.content.domain.CoursePub;
import com.xuecheng.content.domain.Teachplan;
import com.xuecheng.content.domain.TeachplanWork;
import com.xuecheng.content.mapper.TeachplanWorkMapper;
import com.xuecheng.content.service.CourseBaseService;
import com.xuecheng.content.service.CoursePubService;
import com.xuecheng.content.service.TeachplanService;
import com.xuecheng.content.service.TeachplanWorkService;
import com.xuecheng.teaching.api.TeachingClient;
import com.xuecheng.web.exception.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class TeachplanWorkServiceImpl extends ServiceImpl<TeachplanWorkMapper, TeachplanWork> implements TeachplanWorkService {

    @Autowired
    private TeachplanService teachplanService;//课程计划

    @Autowired
    private CourseBaseService courseBaseService;//课程基本信息

    @Autowired
    private CoursePubService coursePubService;//课程发布

    @Autowired
    private TeachingClient teachingClient; //作业feign

    //课程计划绑定作业
    @Override
    public ResponseResult association(BindTeachplanWorkDto bindDto) {
        //前端参数校验
        if (!ObjectUtil.isAllNotEmpty(bindDto.getTeachplanId(),bindDto.getWorkId(),bindDto.getWorkTitle())){
            throw new BusinessException(ErrorCode.PARAMSERROR);
        }
        //查询课程计划
        Teachplan teachplan = teachplanService.getById(bindDto.getTeachplanId());
        //根据课程计划获取课程基本信息
        CourseBase courseBase = courseBaseService.getById(teachplan.getCourseId());
        //根据课程id获取课程发布
        CoursePub coursePub = coursePubService.lambdaQuery().eq(CoursePub::getCourseId, courseBase.getId()).one();
        //校验课程状态 已发布、审核已通过、已提交的不允许再绑定
        if (StrUtil.equals(courseBase.getAuditStatus(), CourseConstants.AUDIT_COMMIT_STATUS)
                || StrUtil.equals(courseBase.getAuditStatus(), CourseConstants.AUDIT_PASTED_STATUS)
                ||  StrUtil.equals(courseBase.getAuditStatus(), CourseConstants.AUDIT_PUBLISHED_STATUS)) {
            throw new BusinessException(ErrorCode.MEDIABINDERROR);
        }
        //远程调用获取作业信息
        ResponseResult<WorkVo> responseResult = teachingClient.findWorkById(bindDto.getWorkId());
        if (responseResult.getCode()!=0){
            throw new BusinessException(ErrorCode.DATAERROR);
        }
        //取出调用结果
        WorkVo workVo = responseResult.getData();
        //封装作业绑定实体
        TeachplanWork teachplanWork = new TeachplanWork();
        teachplanWork.setWorkId(bindDto.getWorkId());//作业id
        teachplanWork.setTeachplanId(teachplan.getId());//课程计划id
        teachplanWork.setWorkTitle(workVo.getTitle());//作业标题
        teachplanWork.setCourseId(courseBase.getId());//课程id
        teachplanWork.setCreateDate(new Date());//创建时间
        if (coursePub!=null){
            teachplanWork.setCoursePubId(coursePub.getId());//课程发布id
        }
        //保存作业绑定表
        this.save(teachplanWork);
        //修改media_type
        teachplan.setMediaType("001004");
        teachplanService.updateById(teachplan);
        //返回结果
        return ResponseResult.okResult();
    }

    //课程计划解绑作业
    @Override
    public ResponseResult deleteWork(Long teachplanWorkId) {
        //前端参数校验
        if (teachplanWorkId==null){
            throw new BusinessException(ErrorCode.PARAMSERROR);
        }
        //根据workId获取绑定对象
        TeachplanWork work = this.getById(teachplanWorkId);
        //根据课程计划获取课程基本信息
        CourseBase courseBase = courseBaseService.getById(work.getCourseId());
        //校验课程状态 已发布、审核已通过、已提交的不允许删除
        if (StrUtil.equals(courseBase.getAuditStatus(), CourseConstants.AUDIT_COMMIT_STATUS)
                || StrUtil.equals(courseBase.getAuditStatus(), CourseConstants.AUDIT_PASTED_STATUS)
                ||  StrUtil.equals(courseBase.getAuditStatus(), CourseConstants.AUDIT_PUBLISHED_STATUS)) {
            throw new BusinessException(ErrorCode.MEDIABINDERROR);
        }
        //删除绑定的作业信息
        this.removeById(teachplanWorkId);
        //返回结果
        return ResponseResult.okResult();
    }
}
